package com.neuedu.controller;

import com.neuedu.dao.impl.TokenDao;
import com.neuedu.factory.ClassFactory;
import com.neuedu.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String token = UUID.randomUUID().toString();
        LoginService ls = (LoginService) ClassFactory.createObj("LoginService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        new TokenDao().setToken(username, token);
        int flag = ls.checkLogin(username, password);
        String res;
        if (flag > 0) {
            res = token;
        } else {
            res = "false";
        }
        response.getWriter().append(res);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
